package com.baidu.ocrgeneralocrdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

public class TestCameraImageActivity extends AppCompatActivity {
    private static final String TAG = "CameraImageTestActivity";

    private Button buttonGetCameraImage = null;
    private ImageView cameraImgView = null;
    private TextView cameraImagePredictResultView = null;
    public static final int RC_CAMETA_IMAGE = 2;

    public static final int PHOTO_REQUEST_CUT = 3;
    private Activity activity = null;
    private Uri imageUri = null;
    private Uri cropImageUri = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_camera_image);

        activity = this;
        buttonGetCameraImage = findViewById(R.id.bt_get_camera_image);
        cameraImgView = findViewById(R.id.camera_image);
        cameraImagePredictResultView = findViewById(R.id.cameta_image_predict_result);
        imageUri = getImageStreamFromExternal("capture.jpg");

        buttonGetCameraImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, RC_CAMETA_IMAGE);
            }
        });
    }

    // 选取图片后进行图片预测
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case RC_CAMETA_IMAGE:
                if (resultCode == RESULT_OK){
                    crop(imageUri);

                }
                break;
            case PHOTO_REQUEST_CUT:{

//                    Bitmap bitmap = data.getParcelableExtra("data");
                    try {
                        // 获得图片原始的地址 没有压缩
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(cropImageUri));
                        PredictorWrapper.asyncTestOneImage(activity, bitmap, cameraImgView, cameraImagePredictResultView, new PredictorResultListener() {
                            @Override
                            public void ResultListener(String result) throws IOException {

                            }
                        });
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }


            }
            break;
        }
    }
// 剪切图片
    private  void  crop(Uri uri){
        File CropPhoto  = new File(getExternalCacheDir(),"crop_image.jpg");
        try{
            if(CropPhoto.exists()){
                CropPhoto.delete();
            }
            CropPhoto.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        cropImageUri = Uri.fromFile(CropPhoto);
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intent.putExtra("crop","true");
        intent.putExtra("aspectX",5);
        intent.putExtra("aspectY",2);
        intent.putExtra("outputFormat",Bitmap.CompressFormat.PNG);
//        intent.putExtra("noFaceDetection",true);
        intent.putExtra("scale",true);
        intent.putExtra("outputX",1000);
        intent.putExtra("outputY",400);
        intent.putExtra("return-data",false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,cropImageUri);

        startActivityForResult(intent,PHOTO_REQUEST_CUT);




    }



    public Uri getImageStreamFromExternal(String imageName) {
        File externalPubPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
        );

        File picPath = new File(externalPubPath, imageName);
        if (!picPath.exists()) {
            try {
                picPath.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Uri uri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(this, "com.baidu.ocrgeneralocrdemo.fileprovider", picPath);
        } else {
            uri = Uri.fromFile(picPath);
        }

        return uri;
    }
}
